package rest;

import java.net.URI;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import rest.entities.User;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.json.JSONConfiguration;
import com.vaadin.Application;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;

public class MyApplication extends Application {

	private Window mainWindow;

	@Override
	public void init() {
		mainWindow = new Window();
		setMainWindow(mainWindow);
		mainWindow.addComponent(new Label("hello world!!!"));

		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
		WebResource service = client.resource(getBaseURI());

		mainWindow.addComponent(new Label(service.path("rest").path("hello")
				.accept(MediaType.TEXT_PLAIN).get(String.class)));
		mainWindow.addComponent(new Label("username:"+service.path("rest").path("hello")
				.accept(MediaType.APPLICATION_JSON).get(User.class).getUsername()));
	}

	private static URI getBaseURI() {
		return UriBuilder.fromUri("http://localhost:8888").build();
	}

}
